<?php
namespace App\Services\SearchService;
/**
 * 筛选API
 *
 * 筛选Service
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
use App\Services\BaseService;
use App\Services\ProductsService\ProductTool;
use App\Services\ProductsService\ProductsService;
use App\Services\UserService\UserTool;
use App\Services\ProductTempService\ProductTempTool;

class SearchService extends BaseService
{
    protected $productTool;
    public function __construct(ProductTool $productTool)
    {
        $this->productTool = $productTool;
    }

    //获取筛选数组
    public function get_search_arr($product_temp_category_id,$app_id){
        $this->setParameter([
            'table' => 'searchs',
            'where' => [['product_temp_categoty_id','=',$product_temp_category_id],['app_id','=',$app_id],['pid','=',0],['state','=',1]],
            'select'=> ['search_id','search_name','app_id','type','is_radio','level'],
            'order' => 'sort',
            'sort'  => 'desc'
        ]);
        $searchs = $this->getAll()->toArray();
        foreach($searchs as $key=>$search){
            //获取子类
            if($search->type == 1){
                //价格
                $search_children = $this->productTool->get_price_interval($product_temp_category_id,$search->level);
            }else{
                $this->setParameter([
                    'table' => 'searchs',
                    'where' => [['pid','=',$search->search_id],['state','=',1]],
                    'select'=> ['search_name','resource_id']
                ]);
                $search_children = $this->getAll()->toArray();
            }
            if(empty($search_children)){
                array_splice($searchs,$key,1);
            }else{
                $search->search_children = $search_children;
            }
        }
        return !empty($searchs) ? $this->createResult('M00000',$searchs) :
            $this->createResult('M00000',array());
    }

    /**
     * 搜索用户和作品
     * @Author    zhouchao
     * @DateTime  2017-03-13
     * @copyright 2017 QuanZhouYunYiShang Inc.
     * @license   [license]
     * @version   [version]
     * @param     integer 0 搜索作品 1 搜索设计书 2 搜索品牌商
     * @param     array 搜索关键字['keyword'=>'1']
     * @param     UserTool
     * @param     ProductTempTool
     * @param     ProductsService
     * @param     integer
     * @return    array
     */
    public function search_for_type($type, $conditions, UserTool $user_tool, ProductTempTool $product_temp_ser, ProductsService $product_ser, $per_page=10)
    {
        if(!is_array($conditions) || !isset($conditions['keyword'])){
            return $this->createResult('M00000',['total_page' => 1, 'per_page' => $per_page, 'current_page' => 1, 'data' => []]);
        }
        // 0 作品 1 用户 2 品牌
        switch ($type) {
            case 0:
                $search_results = $this->search_product($conditions, $user_tool, $product_temp_ser, $product_ser, $per_page);
                break;
            case 1:
            case 2:
                $search_results = $this->search_users($type, $conditions, $user_tool, $per_page);
                break;
            default:
                return $this->createResult('M00000',['total_page' => 1, 'per_page' => $per_page, 'current_page' => 1, 'data' => []]);
                break;
        }
        return $this->createResult('M00000',$search_results);
         
    }

    /**
     * 搜索产品
     * @Author    zhouchao
     * @DateTime  2017-03-13
     * @copyright 2017 QuanZhouYunYiShang Inc.
     * @license   [license]
     * @version   [version]
     * @param     array 搜索关键字['keyword'=>'1']
     * @param     UserTool
     * @param     ProductTempTool
     * @param     ProductsService
     * @param     integer
     * @return    array
     */
    private function search_product($conditions, UserTool $user_tool, ProductTempTool $product_temp_ser, ProductsService $product_ser, $per_page=10)
    {
        //根据用户名和模板名搜索
        $user_ids = $user_tool->search_user_ids($conditions['keyword']);
        $product_ids_by_user_ids = $this->productTool->get_product_ids_by_user_ids($user_ids);
        $product_temp_ids = $product_temp_ser->searchProdTempList($conditions['keyword'],1);
        $product_ids_by_temp_ids = $this->productTool->get_product_ids_by_temp_ids($product_temp_ids);
        $product_ids = array_unique(array_merge($product_ids_by_user_ids,$product_ids_by_temp_ids));
        $search_results = $product_ser->get_list_over_resources_ids($product_ids,$per_page)->toArray();
        return ['total_page' => $search_results['last_page'], 'per_page' => $per_page, 'current_page' => $search_results['current_page'], 'data' => $search_results['data']];
    }

    /**
     * 搜索用户
     * @Author    zhouchao
     * @DateTime  2017-03-13
     * @copyright 2017 QuanZhouYunYiShang Inc.
     * @license   [license]
     * @version   [version]
     * @param     integer 0 搜索作品 1 搜索设计书 2 搜索品牌商
     * @param     array 搜索关键字['keyword'=>'1']
     * @param     UserTool
     * @param     integer
     * @return    array
     */
    public function search_users($type, $conditions, UserTool $user_tool, $per_page=10)
    { 
        $search_results = $user_tool->search_users($type, $conditions['keyword'], $per_page);
        return ['total_page' => $search_results['last_page'], 'per_page' => $per_page, 'current_page' => $search_results['current_page'], 'data' => $search_results['data']];
    }
}